package com.leetcode.sorts.tree;

import java.util.*;

/**
 * @Author: WuPeng
 * @Date: 2021/6/2 7:40 下午
 *
 * 102.二叉树的层序遍历
 *
 * BFS（广度优先）
 *
 */
public class LevelOrder_102 {

    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            List<Integer> level = new ArrayList<>();
            int currentLevelSize = queue.size();
            for (int i = 0; i < currentLevelSize; i++) {
                TreeNode node = queue.poll();
                level.add(node.val);
                if (node.left != null){
                    queue.offer(node.left);
                }
                if (node.right != null){
                    queue.offer(node.right);
                }
            }
            res.add(level);
        }
        return res;
    }

    public static void main(String[] args) {
        TreeNode left_left = new TreeNode(4);
        TreeNode left_right = new TreeNode(5);
        TreeNode right_left = new TreeNode(6);
        TreeNode right_right = new TreeNode(7);
        TreeNode left = new TreeNode(2,left_left,left_right);
        TreeNode right = new TreeNode(3,right_left,right_right);
        TreeNode root = new TreeNode(1,left,right);

        List<List<Integer>> list = levelOrder(root);

        System.out.println(Arrays.toString(list.toArray()));
    }

    public int findMaxLength(int[] nums) {
        int max_len = 0;


        return 0;
    }

}
